//
//  WDGroMoreTipManager.m
//  WDGroMore
//
//  Created by little People on 2023/8/2.
//

#import "WDGroMoreTipManager.h"
#import "WDGroMoreHeader.h"

@interface WDGroMoreTipManager ()
@property (nonatomic, strong) UIWindow *tipWindow;
@end

@implementation WDGroMoreTipManager
{
    WDGroMoreTipView *tipView;
}
static WDGroMoreTipManager *_manager = nil;

+ (instancetype)manager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[self alloc] init];
        
    });
    return _manager;
}

- (void)show
{
    if (self.tipWindow) return;

        self.tipWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.tipWindow.windowLevel = UIWindowLevelAlert + 1; // 保证在所有视图之上
        self.tipWindow.backgroundColor = [UIColor clearColor];
        self.tipWindow.hidden = NO;

        UIViewController *vc = [[UIViewController alloc] init];
        vc.view.backgroundColor = [UIColor clearColor];
        self.tipWindow.rootViewController = vc;

        // 添加拦截视图
        tipView = [[WDGroMoreTipView alloc] initWithFrame:vc.view.bounds];
        tipView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        [vc.view addSubview:tipView];

        // 添加内容视图
        UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)];
        contentView.center = tipView.center;
        contentView.backgroundColor = [UIColor whiteColor];
        contentView.layer.cornerRadius = 8;
        [tipView addSubview:contentView];
    
    UIView *top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, contentView.frame.size.width, 40)];
    top.backgroundColor = [UIColor blueColor];
    [contentView addSubview:top];
    
    UILabel *topLabel = [[UILabel alloc] initWithFrame:top.bounds];
    topLabel.textAlignment = NSTextAlignmentCenter;
    topLabel.textColor = [UIColor whiteColor];
    topLabel.text = @"提示";
    [top addSubview:topLabel];
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, top.frame.size.height+10, contentView.frame.size.width-80, contentView.frame.size.height/2)];
    contentLabel.numberOfLines = 6;
    contentLabel.textAlignment = NSTextAlignmentLeft;
    contentLabel.textColor = [UIColor blackColor];
    contentLabel.text = @"当前检测不到网络\n\n1）请确保当前网络状态畅通\n2）前去设置页面检查网络权限\n3）请等待网络恢复畅通，恢复后此页面自动消失";
    [contentView addSubview:contentLabel];
    
    UIButton *setButton = [[UIButton alloc] initWithFrame:CGRectMake(10, contentView.frame.size.height-50, contentView.frame.size.width-20, 40)];
    [setButton setBackgroundColor:[UIColor blueColor]];
    [setButton setTitle:@"去设置" forState:UIControlStateNormal];
    [setButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    setButton.layer.cornerRadius = 4;
    [setButton addTarget:self action:@selector(goToSetPage:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:setButton];
}

- (void)goToSetPage:(UIButton *)sender
{
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    }

}

// 拦截所有点击事件，防止穿透
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    return YES; // 表示点击事件不向下传递
}

- (void)remove {
    [tipView removeFromSuperview];
    tipView = nil;

    self.tipWindow.hidden = YES;
    self.tipWindow.rootViewController = nil;
    self.tipWindow = nil;
}

@end
